<template>
  <div class="middleware_state">
    <div class="header_title">
      <!-- <span>中间件信息</span> -->
      <b>
        当前单位中间件模式：{{unitConfig.singleMiddleware | getsingleMiddleware}}
        <el-button type="text" icon="el-icon-sort" @click="handleSwitch">切换</el-button>
      </b>
    </div>
    <el-form :inline="true" :model="elform" size="mini" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="middlewareDialog" size="mini" icon="el-icon-plus">添加</el-button>
      </el-form-item>
      <!-- <el-form-item>
        <el-button type="primary" @click="getList" size="mini">查询</el-button>
      </el-form-item>-->
    </el-form>
    <el-table
      v-loading="loading"
      :data="detailslist"
      ref="table"
      border
      highlight-current-row
      :header-cell-style="headFirst"
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column prop="middlewareName" label="中间件名称"></el-table-column>
      <el-table-column prop="middlewareCode" label="中间件编号"></el-table-column>
      <el-table-column prop="whiteListIp" label="ip白名单"></el-table-column>
      <el-table-column prop="whiteListMac" label="mac白名单"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="middlewareDialog(scope.row)" size="small">修改</el-button>
          <el-button
            type="text"
            @click.stop="deleteDate(scope.row)"
            size="small"
            style="color: red"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="list">
      <!-- 编辑班次 -->
      <el-dialog
        width="500px"
        custom-class="edit"
        center
        :before-close="middlewarecloseDialog"
        :title="middlewareTTitle"
        :close-on-click-modal="false"
        :visible.sync="middlewareTVisible"
        append-to-body
      >
        <el-form
          ref="alldata"
          :model="alldata"
          :rules="alldatarule"
          :label-position="labelPosition"
          label-width="100px"
        >
          <el-form-item label="中间件名称:" prop="middlewareName">
            <el-col :span="20">
              <el-input v-model="alldata.middlewareName" size="mini"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="中间件编号:" prop="middlewareCode">
            <el-col :span="20">
              <el-select
                v-model="alldata.middlewareCode"
                placeholder="请选择"
                style="width:100%"
                filterable
                clearable
                size="mini"
              >
                <el-option
                  v-for="item in middlewareCodeoption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="中间件ip:" prop="whiteListIp">
            <el-col :span="20">
              <el-input v-model="alldata.whiteListIp" size="mini"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="中间件mac:" prop="whiteListMac">
            <el-col :span="20">
              <el-input v-model="alldata.whiteListMac" size="mini"></el-input>
            </el-col>
          </el-form-item>
          <!-- <el-form-item>
            <el-col :span="20">
            <el-button type="primary" @click="onSubmit()">保存信息</el-button>
          </el-form-item>-->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="middlewarecloseDialog">取消</el-button>
          <el-button
            type="primary"
            @click="eidtnormalFormSubmit"
            v-if="middlewareTTitle != '新增中间件'"
          >确定</el-button>
          <el-button
            type="primary"
            @click="addnormalFormSubmit"
            v-if="middlewareTTitle == '新增中间件'"
          >确定</el-button>
        </span>
      </el-dialog>
      <!-- <el-form ref="alldata" :model="alldata" :rules="alldatarule"  :label-position="labelPosition" label-width="100px">
                <el-form-item label="中间件ip:" prop="whiteListIp">
                   <el-col :span="4">
                      <el-input v-model="alldata.whiteListIp" size="mini"></el-input>
                   </el-col>    
                </el-form-item>
                <el-form-item label="中间件mac:" prop="whiteListMac">
                   <el-col :span="4">
                      <el-input v-model="alldata.whiteListMac" size="mini"></el-input>
                   </el-col>    
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit()">保存信息</el-button>
                </el-form-item>
      </el-form>-->
    </div>
  </div>
</template>

<script>
// ip验证规则
let ipPort = (rule, value, callback) => {
  let portReg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
  if (!portReg.test(value)) {
    callback(new Error('ip格式错误！'))
  } else {
    callback()
  }
}
export default {
  data() {
    return {
      elform: {},
      loading: false,
      //单位配置信息,识别是否是多中间件
      unitConfig: {},
      // 考勤时段数据
      detailslist: [],
      // 编号
      middlewareCodeoption: [],
      labelPosition: 'right',
      alldata: {
        middlewareCode: '',
        middlewareGuid: '',
        middlewareName: '',
        unitGuid: '',
        whiteListIp: '',
        whiteListMac: ''
      },
      alldatarule: {
        middlewareName: [
          { required: true, message: '请填写中间件名称', trigger: 'blur' }
        ],
        middlewareCode: [
          { required: true, message: '请填写中间件编号', trigger: 'blur' }
        ]
      },
      /**
       * 编辑正常班次
       */
      middlewareTVisible: false,
      middlewareTTitle: '新增中间件'
    }
  },
  created() {
    this.changeschoolid = this.$store.state.user.changeschoolid
    this.userGuid = sessionStorage.getItem('administrator')
    for (let i = 0; i < 32; i++) {
      this.middlewareCodeoption.push({
        value: Math.pow(2, i),
        label: Math.pow(2, i)
      })
    }
    //单位配置信息,识别是否是多中间件
    this.getunitconfiguration()
  },
  mounted() {
    this.getList()
  },
  filters: {
    getsingleMiddleware(val) {
      if (val) {
        return '单中间件'
      } else {
        return '多中间件'
      }
    }
  },
  methods: {
    // 切换模式
    handleSwitch() {
      this.$confirm('此操作会影响本地中间件的数据同步，请谨慎操作,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/middleware/change-middleware'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: {
                isSingleMiddleware: this.unitConfig.singleMiddleware == 0?1:0,
                unitGuid: this.changeschoolid
              },
              messageType: 0
            })
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                message: '切换成功',
                type: 'success'
              })
              this.getunitconfiguration()
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        })
        .catch(() => {})
    },
    //单位配置信息,识别是否是多中间件
    getunitconfiguration() {
      this.$http({
        url: this.$http.adornUrl('/manager/unit-config'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            unitGuid: this.changeschoolid,
            userGuid: this.userGuid
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.unitConfig = { ...data.messageContent }
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },

    /** 获取数据 */
    getList() {
      this.$http({
        url: this.$http.adornUrl('/middleware/list'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        console.log(data)
        if (data.messageCode == 200) {
          this.detailslist = data.messageContent || []
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    /**
     * 编辑
     */
    // 打开弹出
    middlewareDialog(row) {
      /** */
      // 初始化数据
      this.middlewareTVisible = true
      this.$nextTick(() => {
        this.$refs['alldata'].resetFields()
        // this.middlewareTForm.unitGuid = this.changeschoolid
        // 修改中间件
        if (row.middlewareGuid) {
          this.middlewareTTitle = '修改中间件'
          this.alldata = {
            ...row
          }
          // 获取类型，并且获取设备型号，设备参数模板
        } else {
          this.middlewareTTitle = '新增中间件'
        }
      })
    },
    // 关闭弹窗
    middlewarecloseDialog(done) {
      this.middlewareTVisible = false
      // done()
    },
    addnormalFormSubmit() {
      this.alldata.unitGuid = this.changeschoolid
      let portReg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
      if (
        this.alldata.whiteListIp != '' &&
        !portReg.test(this.alldata.whiteListIp)
      ) {
        this.$message.error('中间件ip格式错误')
        return false
      }
      this.$refs['alldata'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/middleware/add-middleware'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: {
                ...this.alldata
              },
              messageType: 0
            })
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.middlewarecloseDialog()
              this.getList()
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        } else {
          return false
        }
      })
    },
    eidtnormalFormSubmit() {
      this.alldata.unitGuid = this.changeschoolid
      let portReg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
      if (
        this.alldata.whiteListIp != '' &&
        !portReg.test(this.alldata.whiteListIp)
      ) {
        this.$message.error('中间件ip格式错误')
        return false
      }
      this.$refs['alldata'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/middleware/update-middleware'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: {
                ...this.alldata
              },
              messageType: 0
            })
          }).then(({ data }) => {
            if (data.messageCode == 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.middlewarecloseDialog()
              this.getList()
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        } else {
          return false
        }
      })
    },
    // 移除
    deleteDate(row) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/middleware/delete-middleware'),
            method: 'post',
            data: this.$http.adornData({
              messageContent: {
                middlewareGuid: row.middlewareGuid,
                unitGuid: this.changeschoolid
              },
              messageType: 0
            })
          }).then(({ data }) => {
            console.log(data)
            if (data.messageCode == 200) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getList()
            } else {
              this.$message.error(data.errorMessage)
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.middleware_state {
  .demo-form-inline {
    padding-top: 10px;
  }
  padding: 10px;
  .header_title {
    //  text-indent: 22px;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #e5e9f2;
    color: #909399;
  }
  .list {
    font-size: 16px;
    font-weight: bold;
    color: #909399;
    .el-form-item {
      margin-bottom: 0px;
      padding-top: 10px;
      // border-bottom: 1px solid #e5e9f2;
      width: 100%;
    }
    .el-form-item:last-child {
      border-bottom: 0px solid #e5e9f2;
    }
  }
}
</style>